<template>
  <div ref="barRef" style="width: 100%; height: 300px" />
</template>

<script>
import * as echarts from 'echarts'
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_color.js'
import ResizeObserver from 'resize-observer-polyfill'
export default {
  data() {
    return {
      chartInstance: null,
      option: {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // Use axis to trigger tooltip
            type: 'shadow', // 'shadow' as default; can also be 'line' or 'shadow'
          },
        },
        color: ['#5c7bef', '#6ecfa3', '#fa9b37'],
        legend: { show: false },
        grid: {
          left: '12%',
          right: '4%',
          bottom: '5%',
          top: '5%',
          containLabel: false,
        },
        xAxis: {
          show: false,
          type: 'value',
        },
        yAxis: {
          type: 'category',
          axisTick: false,
          data: [],
        },
        series: [
          {
            name: 'HTTP',
            type: 'bar',
            barMaxWidth:'50',
            stack: 'total',
            label: {
              show: true,
            },
            emphasis: {
              focus: 'series',
            },
            data: [],
          },
          {
            name: 'FTP',
            type: 'bar',
            barMaxWidth:'50',
            stack: 'total',
            label: {
              show: true,
            },
            emphasis: {
              focus: 'series',
            },
            data: [],
          },
          {
            name: '未知',
            type: 'bar',
            barMaxWidth:'50',
            stack: 'total',
            label: {
              show: true,
            },
            // barWidth:'40',
            emphasis: {
              focus: 'series',
            },
            data: [],
          },
        ],
      },
    }
  },
  computed: {
    ...mapState({
      sideTheme: (state) => state.settings.sideTheme,
    }),
  },
  watch: {
    sideTheme: {
      handler(newVal) {
        this.$nextTick(() => {
          this.updateChart()
        })
      },
      immediate: true,
    },
  },
  mounted() {
    this.initChart()
    const resizeOb = new ResizeObserver((entries) => {
      for (const entry of entries) {
        echarts.getInstanceByDom(entry.target)?.resize()
      }
    })
    resizeOb.observe(this.$refs.barRef)
    // 添加随页面变化而变化
    window.addEventListener('resize', () => {
      this.chartInstance.resize()
    })
  },
  destroyed() {
    window.removeEventListener('resize', () => {
      this.chartInstance.resize()
    })
  },
  methods: {
    initChart() {
      // 初始化对象
      this.chartInstance = echarts.init(this.$refs.barRef)
      this.chartInstance.setOption(this.option, true)
      window.addEventListener('resize', () => {
        this.chartInstance.resize()
      })
    },
    getData({yAxisData=[],seriesData}) {
      if(yAxisData.length>0){
        this.option.yAxis.data=yAxisData
      }
    this.option.series.forEach((i,index)=>{
     if(index==0){
       i.data=seriesData.HTTP
     }else if(index==1){
      i.data=seriesData.FTP
     }else{
      i.data=seriesData.OTHER
     }
    })
      this.chartInstance.setOption(this.option, true)
      this.updateChart()
    },
    updateChart() {
      const titleThemeDark = getThemeValue('titleThemeDark')
      const DarkThemeStyle = {
        title: {
          textStyle: {
            color: titleThemeDark.titleColor,
          },
        },
        legend: {
          textStyle: {
            color: titleThemeDark.titleColor,
          },
        },
        xAxis: {
          axisLabel: {
            color: titleThemeDark.titleColor,
          },
          axisLine: {
            show: false,
          },
        },
        yAxis: {
          axisLabel: {
            color: titleThemeDark.titleColor,
          },
          splitLine: {
            lineStyle: {
              color: ' rgba(0,0,0,0.15)',
            },
          },
          axisLine: {
            show: false,
          },
        },
      }
      const titleThemeHigh = getThemeValue('titleThemeHigh')
      const HighThemeStyle = {
        title: {
          textStyle: {
            color: titleThemeHigh.titleColor,
          },
        },
        legend: {
          textStyle: {
            color: titleThemeHigh.titleColor,
          },
        },
        xAxis: {
          axisLabel: {
            color: 'rgba(0,0,0,0.45)',
          },
          axisLine: {
            show: false,
          },
        },
        yAxis: {
          axisLabel: {
            color: ' rgba(0,0,0,0.45)',
          },
          axisLine: {
            show: false,
          },
        },
      }
      if (this.sideTheme === 'theme-dark' || this.sideTheme === 'tech') {
        this.chartInstance.setOption(DarkThemeStyle)
      } else {
        this.chartInstance.setOption(HighThemeStyle)
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.visit-app {
  width: 100%;
  height: 100%;
}
</style>
